<?php
header ('Content-type: text/html; charset=utf-8');
if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}
if($_GET['op'] == 'submit'){
    if(empty($_FILES['package'])){
        echo '上传文件不能为空';
        exit;
    }
    $packageName     = $_FILES['package']['name'];
    $packageSize     = $_FILES['package']['size'];
    $packageErrorNum = $_FILES['package']['error'];  
    $packageTmpName  = $_FILES['package']['tmp_name'];
   
    
    if($_FILES['package']['size']>80000*1024*1024){
      showmessage('文件太大','admin.php?ac=pack_emotion');
    }

    if(!move_uploaded_file($packageTmpName, './upload/'.$packageName) || $packageErrorNum > 0){      
        showmessage('上传失败','admin.php?ac=pack_emotion');
    }
    //ID
    $id   =$_POST['id'];

    $arrtitle=explode('.',$packageName);
    //title
    $title=urlencode($arrtitle[0]);
    
    if(array_pop($arrtitle) != 'zip'){      
        showmessage('格式不正确','admin.php?ac=pack_emotion');     
    }
    //name
    $name=urlencode($_POST['name']);
  
    //解压
    $url_unzip='http://package.ffrj.net/unzip?title='.$title.'&id='.$id;
    $res=json_decode(getHttpResponsePOST($url_unzip),TRUE);
  
        if($res['resultData']!=1){  
        showmessage('解压失败','admin.php?ac=pack_emotion');      
    }
  
    //打包并上传CDN
    $url_pack='http://package.ffrj.net/pack?id='.$id.'&name='.$name.'&title='.$title;

    $res=json_decode(getHttpResponsePOST($url_pack),TRUE);
  
    if(empty($res['resultData'])){
        showmessage('打包失败','admin.php?ac=pack_emotion');
    }
    $m=array_combine($res['resultData']['m_key'],$res['resultData']['m_val']);
  
    $sid        = $res['resultData']['info']['id'];
    $title      = $res['resultData']['info']['title'];
    $name       = $res['resultData']['info']['name'];
    $priview    = JsonFormat_m($res['resultData']['s']);
    $linkinfo   = JsonFormat_m($m);
    $linkzip    = $res['resultData']['zip'];
    $cover      = $res['resultData']['cover'];

    //打包完成的数据插入数据表
    $param=array(
        'do'        =>'pack',
        'action'    =>'insertPackMsg',
        'format'    => 'json',
        'sid'       => $sid,
        'title'     => $title,
        'name'      => $name,
        'priview'   => $priview,
        'linkinfo'  => $linkinfo,
        'linkzip'   => $linkzip,
        'cover'     => $cover,
        
    );
   
    $content =json_decode(get_api_content($_SC ['apiurl'], $param),TRUE);
   
    if(!$content['resultData']){
      showmessage('插入数据库失败','admin.php?ac=pack_emotion');
    }  
   showmessage('打包成功','admin.php?ac=pack_emotion');
   
}else{
//获取最新的表情Id
    $param = array(
    'do'        =>  'pack',
    'action'    =>  'getLastEmotionId',
    'format'    =>  'json',
    );
$content =json_decode(get_api_content($_SC ['apiurl'], $param),TRUE);
$lastEmotionId = $content['resultData'];
$newEmotionId = $lastEmotionId +1;
//获取数据表中是否有没有发布的包
    $param = array(
        'do'        =>  'pack',
        'action'    =>  'getLastPackage',
        'format'    =>  'json',  
        'sid'       =>  $newEmotionId          
    );
    $content =json_decode(get_api_content($_SC ['apiurl'], $param),TRUE);
    $content = $content['resultData'];
   
    //返回数据展示
    $arrthumb=gthumb($content['sid'], $content['name'],json_decode($content['linkinfo'],TRUE), json_decode($content['priview'],TRUE));
    $data=array(
        'id'            => $content['sid'],
        'name'          => $content['title'],
        'cover'         => $content['cover'],
        'download_url'  => $content['linkzip'],
        'thumbnail_m'   => $arrthumb['m'],
        'thumbnail_s'   => $arrthumb['s'],
        'english_name'  => $content['name']
    );
    
}